package com.meituan.catering.management.product.biz.model.converter;

import com.google.common.collect.Lists;
import com.meituan.catering.management.common.helper.StatusHelper;
import com.meituan.catering.management.common.model.api.thrift.AuditingThriftModel;
import com.meituan.catering.management.common.model.biz.AuditingBO;
import com.meituan.catering.management.product.api.thrift.model.dto.ProductDetailThriftDTO;
import com.meituan.catering.management.product.api.thrift.model.response.ProductDetailThriftResponse;
import com.meituan.catering.management.product.biz.model.ProductAccessoryBO;
import com.meituan.catering.management.product.biz.model.ProductDetailBO;
import com.meituan.catering.management.product.biz.model.ProductMethodBO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author Believe
 * Date 2022/5/29 10:35
 */
public class ProductDetailThriftDTOConverter {
    public static ProductDetailThriftDTO toProductDetailThriftDTO(ProductDetailBO productDetailBO) {
        if (Objects.isNull(productDetailBO)) {
            return null;
        }
        ProductDetailThriftDTO productDetailThriftDTO = new ProductDetailThriftDTO();
        productDetailThriftDTO.setId(productDetailBO.getProductBO().getId());
        productDetailThriftDTO.setTenantId(productDetailBO.getProductBO().getTenantId());
        productDetailThriftDTO.setVersion(productDetailBO.getProductBO().getVersion());
        productDetailThriftDTO.setAuditing(toAuditingThriftModel(productDetailBO.getProductBO().getAuditing()));
        productDetailThriftDTO.setName(productDetailBO.getProductBO().getName());
        productDetailThriftDTO.setUnitPrice(productDetailBO.getProductBO().getUnitPrice().doubleValue());
        productDetailThriftDTO.setUnitOfMeasure(productDetailBO.getProductBO().getUnitOfMeasure());
        productDetailThriftDTO.setMinSalesQuantity(productDetailBO.getProductBO().getMinSalesQuantity().doubleValue());
        productDetailThriftDTO.setIncreaseSalesQuantity(productDetailBO.getProductBO().getIncreaseSalesQuantity().doubleValue());
        productDetailThriftDTO.setDescription(productDetailBO.getProductBO().getDescription());
        productDetailThriftDTO.setEnabled(productDetailBO.getProductBO().getEnabled());
        productDetailThriftDTO.setMethodGroups(toMethodGroup(productDetailBO.getMethodBOList()));
        productDetailThriftDTO.setAccessoryGroups(toAccessoryGroup(productDetailBO.getAccessoryBOList()));
        return productDetailThriftDTO;
    }

    public static List<ProductDetailThriftDTO> toProductDetailThriftDTOS(List<ProductDetailBO> productDetailBOList) {
        if (CollectionUtils.isEmpty(productDetailBOList)) {
            return Collections.emptyList();
        }
        List<ProductDetailThriftDTO> detailThriftDTOS = Lists.newLinkedList();
        for (ProductDetailBO productDetailBO : productDetailBOList) {
            System.out.println("productDetailBO:" + productDetailBO);
            ProductDetailThriftDTO productDetailThriftDTO = new ProductDetailThriftDTO();
            productDetailThriftDTO.setId(productDetailBO.getProductBO().getId());
            productDetailThriftDTO.setTenantId(productDetailBO.getProductBO().getTenantId());
            productDetailThriftDTO.setVersion(productDetailBO.getProductBO().getVersion());
            productDetailThriftDTO.setAuditing(toAuditingThriftModel(productDetailBO.getProductBO().getAuditing()));
            productDetailThriftDTO.setName(productDetailBO.getProductBO().getName());
            productDetailThriftDTO.setUnitPrice(productDetailBO.getProductBO().getUnitPrice().doubleValue());
            productDetailThriftDTO.setUnitOfMeasure(productDetailBO.getProductBO().getUnitOfMeasure());
            productDetailThriftDTO.setMinSalesQuantity(productDetailBO.getProductBO().getMinSalesQuantity().doubleValue());
            productDetailThriftDTO.setIncreaseSalesQuantity(productDetailBO.getProductBO().getIncreaseSalesQuantity().doubleValue());
            productDetailThriftDTO.setDescription(productDetailBO.getProductBO().getDescription());
            productDetailThriftDTO.setEnabled(productDetailBO.getProductBO().getEnabled());
            productDetailThriftDTO.setMethodGroups(toMethodGroup(productDetailBO.getMethodBOList()));
            productDetailThriftDTO.setAccessoryGroups(toAccessoryGroup(productDetailBO.getAccessoryBOList()));
            detailThriftDTOS.add(productDetailThriftDTO);
        }
        return detailThriftDTOS;
    }

    public static List<ProductDetailThriftResponse> toResponseList(List<ProductDetailThriftDTO> detailThriftDTOS) {
        if (CollectionUtils.isEmpty(detailThriftDTOS)) {
            return Collections.emptyList();
        }
        List<ProductDetailThriftResponse> responses = Lists.newLinkedList();
        for (ProductDetailThriftDTO detailThriftDTO : detailThriftDTOS) {
            ProductDetailThriftResponse response = new ProductDetailThriftResponse();
            response.setStatus(StatusHelper.success());
            response.setData(detailThriftDTO);
            responses.add(response);
        }
        return responses;
    }

    public static List<ProductDetailThriftDTO.AccessoryGroup> toAccessoryGroup(List<ProductAccessoryBO> accessoryBOList) {
        if (CollectionUtils.isEmpty(accessoryBOList)) {
            return Collections.emptyList();
        }
        List<ProductDetailThriftDTO.AccessoryGroup> groupList = Lists.newLinkedList();
        for (ProductAccessoryBO productAccessoryBO : accessoryBOList) {
            ProductDetailThriftDTO.AccessoryGroup accessoryGroup = new ProductDetailThriftDTO.AccessoryGroup();
            accessoryGroup.setName(productAccessoryBO.getGroupName());
            accessoryGroup.setOptions(toOption(productAccessoryBO));
            groupList.add(accessoryGroup);
        }
        return groupList;
    }

    public static List<ProductDetailThriftDTO.AccessoryGroup.Option> toOption(ProductAccessoryBO productAccessoryBO) {
        if (Objects.isNull(productAccessoryBO)) {
            return null;
        }
        List<ProductDetailThriftDTO.AccessoryGroup.Option> optionList = Lists.newLinkedList();
        ProductDetailThriftDTO.AccessoryGroup.Option option = new ProductDetailThriftDTO.AccessoryGroup.Option();
        option.setId(productAccessoryBO.getId());
        option.setName(productAccessoryBO.getName());
        option.setUnitPrice(productAccessoryBO.getUnitPrice().doubleValue());
        option.setUnitOfMeasure(productAccessoryBO.getUnitOfMeasure());
        optionList.add(option);
        return optionList;
    }

    public static List<ProductDetailThriftDTO.MethodGroup> toMethodGroup(List<ProductMethodBO> methodBOList) {
        if (CollectionUtils.isEmpty(methodBOList)) {
            return Collections.emptyList();
        }
        List<ProductDetailThriftDTO.MethodGroup> groups = Lists.newLinkedList();
        for (ProductMethodBO productMethodBO : methodBOList) {
            ProductDetailThriftDTO.MethodGroup methodGroup = new ProductDetailThriftDTO.MethodGroup();
            methodGroup.setName(productMethodBO.getGroupName());
            methodGroup.setOptions(toOption(productMethodBO));
            groups.add(methodGroup);
        }
        return groups;
    }

    public static List<ProductDetailThriftDTO.MethodGroup.Option> toOption(ProductMethodBO productMethodBO) {
        if (Objects.isNull(productMethodBO)) {
            return null;
        }
        List<ProductDetailThriftDTO.MethodGroup.Option> optionList = Lists.newLinkedList();
        ProductDetailThriftDTO.MethodGroup.Option option = new ProductDetailThriftDTO.MethodGroup.Option();
        option.setId(productMethodBO.getId());
        option.setName(productMethodBO.getName());
        optionList.add(option);
        return optionList;
    }

    public static AuditingThriftModel toAuditingThriftModel(AuditingBO auditingBO) {
        if (Objects.isNull(auditingBO)) {
            return null;
        }
        AuditingThriftModel auditingThriftModel = new AuditingThriftModel();
        auditingThriftModel.setCreatedBy(auditingBO.getCreatedBy());
        auditingThriftModel.setCreatedAtTimestamp(auditingBO.getCreatedAt().getTime());
        auditingThriftModel.setLastModifiedBy(auditingBO.getLastModifiedBy());
        auditingThriftModel.setLastModifiedAtTimestamp(auditingBO.getLastModifiedAt().getTime());
        return auditingThriftModel;
    }
}
